package com.hust360.percent.pojo;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import com.hust360.percent.PercentApplication;
import com.hust360.percent.pojo.Liked.LikedDAO;

public class LikedDAOImpl implements LikedDAO {

	@Override
	public long save(Liked liked) {
		if (liked == null) {
			return -1;
		}
		ContentValues values = new ContentValues();
		values.put(Liked.ID, liked.id);
		values.put(Liked.SHOP_ID, liked.shopId);
		values.put(Liked.UID, liked.uid);

		long result = -1;
		synchronized (PercentApplication.localDB) {
			result = PercentApplication.localDB.insertWithOnConflict(
					Liked.TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
		}
		return result;
	}

	@Override
	public void removeByUID(Long uid) {
		if (uid == null) {
			return;
		}

		synchronized (PercentApplication.localDB) {
			PercentApplication.localDB.delete(Liked.TABLE, Liked.UID + "='"
					+ uid + "'", null);
		}
	}

	@Override
	public void removeByShopId(Long shopId) {
		if (shopId == null)
			return;
		synchronized (PercentApplication.localDB) {
			PercentApplication.localDB.delete(Liked.TABLE, Liked.SHOP_ID + "='"
					+ shopId + "'", null);
		}
	}
}
